package com.weike.wk_android.widget.adapter;

import com.trello.rxlifecycle3.components.support.RxFragment;
import com.weike.wk_android.model.InformationTab;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

/**
 * Information页面ViewPager Adapter
 */
public class TabPagerAdapter extends FragmentStatePagerAdapter {
    private FragmentManager fragmentManager;
    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragmentManager = fm;
    }

    @Override
    public Fragment getItem(int position) {
        RxFragment fragment = null;
        try {
           fragment = (RxFragment) InformationTab.getFragments()[position].newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        return fragment;
    }

    @Override
    public int getCount() {
        return InformationTab.getFragments().length;
    }


    /**
     * 返回TabLayout需要的Title(因为关联ViewPager时会调用
     * populateFromPagerAdapter()会执行removeAllTabs();
     * 然后又从getPageTitle()方法中添加新的tab)
     * @param position
     * @return
     */
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return InformationTab.getTabsTxt()[position];
    }
}
